// 导入数据库处理模块
const { restart } = require('nodemon')
const db = require('../db')

// 添加分类
exports.addcategory = (req,res)=>{ 
    // 判断是否存在
    const sqlstr1 = 'select * from yeb_category where cate_name = ?'
    db.query(sqlstr1,req.body.cate_name,(err,result)=>{ 
        if(err) return res.echo(err)
        if(result.length === 1) return res.echo('该类名已存在，请修改后重试...')
        // 若不存在则插入
        const sqlstr2 = 'insert into yeb_category set ?'
        db.query(sqlstr2,req.body,(err,result)=>{ 
            if(err) return res.echo(err)
            if(result.affectedRows !==1 ) return res.echo('添加失败！')
            res.echo('添加成功！',0)
        })
    })
}

// 获取分类
exports.getcategory = (req,res)=>{
     const sqlstr = 'select * from yeb_category'
     db.query(sqlstr,(err,result)=>{ 
        if(err) return res.echo(err) 
        if(result.length == 0 ) return res.echo('暂无商品分类')
        res.send({
            status:0,
            message:'商品分类获取成功！',
            data:result
        }) 
     })
}

// 修改分类
exports.updatecategory = (req,res)=>{ 
    // 判断是否存在
    const sqlstr1 = 'select * from yeb_category where cate_name = ?'
    db.query(sqlstr1,req.body.cate_name,(err,result)=>{ 
        if(err) return res.echo(err)
        if(result.length === 1) return res.echo('类名已重复！')
        const sqlstr2 = 'update yeb_category set cate_name=?,cate_alias = ? where id = ?'
        console.log(req.body);
        db.query(sqlstr2,[req.body.cate_name,req.body.cate_alias,req.body.id],(err,result)=>{ 
            if(err) return res.echo(err) 
            if(result.affectedRows !==1 ) return res.echo('商品分类更新失败！')
            res.echo('商品分类更新成功！', 0)
        })
    })
}

// 删除分类
exports.delcategory = (req,res)=>{
    const sqlstr = 'delete from yeb_category where id = ?'
    db.query(sqlstr,req.body.id,(err,result)=>{ 
        if(err) return res.echo(err)
        if(result.affectedRows !==1 ) return res.echo('商品分类删除失败！')
        res.echo('商品分类删除成功！',0)
    })
}

// 发布商品信息的处理函数
exports.publishinfo = (req,res)=>{ 
   const sqlstr1 = 'insert into yeb_product_info set ?'
    db.query(sqlstr1,req.body,(err,result)=>{ 
        if(err) return res.echo(err)
        if(result.affectedRows !== 1) return res.echo('商品发布失败！')
        res.echo('商品发布成功！',0)
    })
}

//更新商品信息
exports.updatestoreinfo = (req,res) => {
    const sqlstr = 'update yeb_product_info set product_name = ?, product_img = ?, price = ?, detail = ?, product_num = ?, category = ? where id = ?'
    const store = req.body
    // console.log(store)
    db.query(sqlstr,[store.product_name,store.product_img,store.price,store.detail,store.product_num,store.category,store.id],(err,result) => {
        if(err) return res.echo(err)
        if(result.affectedRows !== 1) return res.echo('商品信息编辑失败')
        res.echo('商品信息编辑成功', 0)
    })
} 

//  将商品设置为热门 - 取消热门
exports.setHot =(req,res) => {
    const sqlstr = 'update yeb_product_info set hot=? where id =?'
    db.query(sqlstr,[req.body.hot,req.body.id],(err,result)=>{ 
        if(err) return res.echo(err)
        if(result.affectedRows !== 1) return res.echo('推荐热门失败')
        if(req.body.hot == 1){
            res.echo('热门推荐成功',0)
        } else {
            res.echo('已取消热门推荐',0)
        }
    })
}

// 获取热门商品 
exports.getHotstore = (req,res) =>{
    const sqlstr = 'select * from yeb_product_info where hot = 1'
    db.query(sqlstr,(err,result) => {
        if(err) return res.echo(err)
        if(result.length == 0) return res.echo('暂无热门商品')
        res.send({
            status:0,
            data:result,
            message:'热门商品获取成功'
        })
    })
}

// 查询全部商品信息 - 已审核
exports.getstoreinfo = (req,res)=>{ 
    const sqlstr = 'select * from yeb_product_info where isAudit = 1'
    db.query(sqlstr,(err,result)=>{ 
        if(err) return res.echo(err)
        if(result.length == 0) return res.echo('暂无商品信息')
        res.send({ 
            status:0,
            data:result,
            message:'商品信息获取成功'
        })
    })
}

// 查询全部商品信息 - 待审核
exports.getstoreinfoaudit = (req,res)=>{ 
    const sqlstr = 'select * from yeb_product_info where isAudit = 0'
    db.query(sqlstr,(err,result)=>{ 
        if(err) return res.echo(err)
        if(result.length == 0) return res.echo('暂无商品信息')
        res.send({ 
            status:0,
            data:result,
            message:'商品信息获取成功'
        })
    })
}

// 审核通过
exports.isaudit = (req,res)=>{ 
    const sqlstr = 'update yeb_product_info set isAudit = 1 where id =?'
    db.query(sqlstr,req.body.id,(err,result)=>{ 
        if(err) return res.echo(err)
        if(result.affectedRows === 0) return res.echo('审核失败')
        res.echo('已通过审核！',0)
    })
}
// 审核不通过并添加原因
exports.isauditaddreason = (req,res) => {
    const sqlstr = 'update yeb_product_info set isAudit = 2,isAuditReason=? where id =?'
    db.query(sqlstr,[req.body.reason,req.body.id],(err,result)=> {
        if(err) return res.echo(err)
        if(result.affectedRows === 0) return res.echo('审核失败')
        res.echo('已审核',0)
    })
}

// 查询商品详情 - id 
exports.storeinfobyid = (req,res)=>{ 
    const sqlstr = 'select * from yeb_product_info where id = ?'
    db.query(sqlstr,req.body.id,(err,result)=>{ 
        if(err) return res.echo(err)
        if(result.length == 0) return res.echo('商品查询失败！')
        res.send({ 
            data:result,
            status:0,
            message:'商品查询成功！'
        })
    })
}

// 按照分类查询商品数据
exports.storeinfobycategory = (req,res)=>{ 
    const sqlstr = 'select * from yeb_product_info where category = ?'
    db.query(sqlstr,req.body.category,(err,result)=>{ 
        if(err) return res.echo(err)
        if(result.length == 0) return res.echo('商品查询失败！')
        res.send({
            data:result,
            status:0,
            message:'商品查询成功！'
        })
    })
}

// 按照关键词查询商品
exports.getstoreinfobykeyword = (req,res) => {
    const sqlstr = "SELECT * FROM yeb_product_info where product_name like ? "
    const keyword ='%' + req.body.keyword + '%'
    db.query(sqlstr,keyword,(err,result) => {
        if(err) return res.echo(err)
        if(result.length === 0) return res.echo('目前没有符合条件的商品')
        res.send({ 
            status:0,
            message:'商品查询成功',
            data: result
        })
    })
}

// 收藏或取消收藏商品
exports.starStore = (req,res) => {
    const sqlstr = 'select star from yeb_product_info where id =?'
    db.query(sqlstr,req.body.id,(err,result)=> {
        if(err) return res.echo(err)
        if(result[0].star === 0) {
            const sqlstr1 = 'update yeb_product_info set star = 1 where id =?'
            db.query(sqlstr1,req.body.id,(err,result)=> {
                if(err) return res.echo(err)
                if(result.affectedRows ===0) return res.echo('error')
                res.echo('收藏成功',0)
            })
        } else {
            const sqlstr1 = 'update yeb_product_info set star = 0 where id =?'
            db.query(sqlstr1,req.body.id,(err,result)=> {
                if(err) return res.echo(err)
                if(result.affectedRows ===0) return res.echo('error')
                res.echo('已取消收藏',0)
            })
        }
    })
}
// 加入购物车
exports.addshoppingcar = (req,res) => {
    const sqlstr = 'select shoppingcar from yeb_product_info where id =?'
    db.query(sqlstr,req.body.id,(err,result)=> {
        if(err) return res.echo(err)
        if(result[0].shoppingcar === 0) {
            const sqlstr1 = 'update yeb_product_info set shoppingcar = 1 where id =?'
            db.query(sqlstr1,req.body.id,(err,result)=> {
                if(err) return res.echo(err)
                if(result.affectedRows ===0) return res.echo('error')
                res.echo('加入购物车成功',0)
            })
        } else {
            res.echo('该商品已加入购物车，请勿重复操作')
        }
    })
}
// 获取购物车的商品
exports.getshoppingcarstore = (req,res) => {
    const sqlstr = 'select * from yeb_product_info where shoppingcar = 1'
    db.query(sqlstr, (err,result) => { 
        if(err) return res.echo(err)
        if(result.length === 0) return res.echo('购物车暂无商品')
        res.send({
            status: 0,
            message: '购物车获取成功',
            data: result
        }) 
    })
}
// 删除购物车商品
exports.removeshoppingcar = (req,res) => {
    const sqlstr = 'update yeb_product_info set shoppingcar = 0 where id = ?'
    db.query(sqlstr,req.body.id,(err,result)=> {
        if(err) return res.echo(err)
        if(result.affectedRows === 0) return res.echo('移除购物车失败')
        res.echo('已移出购物车',0)
    })
}
// 怎加购物车数量
exports.addShoopping = (req,res)=> {
    const sqlstr = 'select num from yeb_product_info where id = ?'
    db.query(sqlstr,req.body.id,(err,result) => {
        if(err) return res.echo(err)
        let addnum = result[0].num 
        addnum ++ 
        const sqlstr1 = 'update yeb_product_info set num = ? where id = ?'
        db.query(sqlstr1,[addnum,req.body.id],(err,result) => {
            if(err) return res.echo(err) 
            if(result.affectedRows === 0) return res.echo('增加失败')
            res.echo('增加成功',0)
        })
    })
}

// 减少购物车
exports.lowShoopping = (req,res)=> {
    const sqlstr = 'select num from yeb_product_info where id = ?'
    db.query(sqlstr,req.body.id,(err,result) => {
        if(err) return res.echo(err)
        let addnum = result[0].num 
        if(addnum > 1) { 
            addnum -- 
        } else {
            addnum = 1
        }
        const sqlstr1 = 'update yeb_product_info set num = ? where id = ?'
        db.query(sqlstr1,[addnum,req.body.id],(err,result) => {
            if(err) return res.echo(err) 
            if(result.affectedRows === 0) return res.echo('减少失败')
            res.echo('减少成功',0)
        })
    })
}
// 获取收藏的商品
exports.getstarstore = (req,res) => {
    const sqlstr = 'select * from yeb_product_info where star = 1'
    db.query(sqlstr,(err,result) => {
        if(err) return res.echo(err) 
        if(result.length === 0) return res.echo('暂无收藏商品')
        res.send({ 
            status:0,
            message:'收藏商品获取成功',
            data: result
        })
    })
}


//删除商品信息
exports.deletstoreinfo = (req,res)=>{ 
    const sql = 'delete from yeb_comment where store_id = ?'
    db.query(sql,req.body.id,(err,result)=>{ 
        if(err) return res.echo(err)
        const sqlstr = 'delete from yeb_product_info where id = ?'
        db.query(sqlstr,req.body.id,(err,result)=>{ 
            if(err) return res.echo(err)
            if(result.affectedRows !== 1){ 
                return res.echo('商品信息删除失败')
            }
            res.echo('商品信息删除成功',0)
        })
        })
}

// 获取全部用户信息
exports.getalluserinfo = (req,res)=>{ 
    const sqlstr = 'select id,username,nickname,email,user_pic,usertype from yeb_user where id != 1 and id !=?'
    db.query(sqlstr,req.user.id,(err,result)=>{ 
        if(err) return res.echo(err)
        if(result.length == 0) res.echo('暂无用户信息')
        res.send({
            data:result,
            message:'用户信息查询成功',
            status:0
        })
    })
}

// 修改用户权限 -  修改为管理员
exports.changeUsertypetoadmin = (req,res) => {
    const sqlstr = 'update yeb_user set usertype="管理员" where id = ?'
    const bodyinfo = req.body
    // console.log(bodyinfo);
    db.query(sqlstr,[bodyinfo.id],(err,result)=>{
        if(err) return res.echo(err)
        if(result.affectedRows !== 1) return res.echo('用户权限修改失败')
        res.echo(`已将该用户变更为管理员`,0)
    })
}

// 修改用户权限 -  撤销管理员身份
exports.changeUsertypetouser = (req,res) => {
    const sqlstr = 'update yeb_user set usertype="用户" where id = ?'
    const bodyinfo = req.body
    // console.log(bodyinfo);
    db.query(sqlstr,[bodyinfo.id],(err,result)=>{
        if(err) return res.echo(err)
        if(result.affectedRows !== 1) return res.echo('用户权限修改失败')
        res.echo(`已将该用户变更为普通用户`,0)
    })
}


// 删除用户
exports.deleteuser = (req,res) => {
    const sqlstr = 'delete from yeb_user where id = ?'
    // console.log(req.body.id);
    db.query(sqlstr,req.body.id,(err,result)=>{
        if(err) return res.echo(err)
        if(result.affectedRows !== 1) return res.echo('删除用户失败')
        res.echo('删除用户成功',0)
    })
}

// 上传轮播图
exports.uploadimg = (req,res) =>{ 
    const sqlstr = 'insert into yeb_images set ?'
    // console.log(req.body)
    db.query(sqlstr,req.body,(err,result)=>{
        if(err) return res.echo(err) 
        if(result.affectedRows !== 1) return res.echo('轮播图添加失败')
        res.echo('轮播图添加成功',0)
    })
}

// 删除轮播图
exports.deleteimages = (req,res) => {
    const sqlstr = 'delete from yeb_images where id =?'
    db.query(sqlstr,req.body.id,(err,result)=>{
        if(err) return res.echo(err)
        if(result.affectedRows == 0) return res.echo('轮播图删除失败')
        res.echo('轮播图删除成功',0)
    })
}
// 修改轮播图信息
exports.updateimages = (req,res) => {
    const sqlstr = 'update yeb_images set img_describe = ?, img_url = ? where id = ?'
    const imginfo = req.body
    // console.log(imginfo.img_url,imginfo.describe,imginfo.id);
    db.query(sqlstr,[imginfo.img_describe,imginfo.img_url,imginfo.id],(err,result) => {
        if(err) return res.echo(err)
        if(result.affectedRows !== 1) return res.echo('轮播图信息编辑失败')
        res.echo('轮播图信息编辑成功',0)
    })
}

// 获取轮播图信息
exports.getimages = (req,res) => {
    const sqlstr = 'select * from yeb_images'
    db.query(sqlstr,(err,result)=>{ 
        if(err) return res.echo(err)
        if(result.length == 0) res.echo('暂无轮播图')
        res.send({
            status: 0,
            message:'轮播图信息查询成功',
            data:result
        })
    })
}

// 轮播图首页是否显示
exports.showimages = (req,res) => {
    const sqlstr = 'update yeb_images set img_show = ? where id =?'
    // console.log(req.body)
    db.query(sqlstr,[req.body.img_show,req.body.id],(err,result) => {
        if(err) return res.echo(err)
        if(result.affectedRows !==1) return res.echo('首页显示设置失败')
        if(req.body.img_show == 1) {
            res.echo('首页显示设置成功',0)
        } else { 
            res.echo('已取消首页显示',0)
        }
    })
}

// 获取首页显示的轮播图
exports.isshowimages = (req,res) => {
    const sqlstr = 'select * from yeb_images where img_show = 1'
    db.query(sqlstr,(err,result)=>{
        if(err) return res.echo(err)
        if(result.length == 0) return res.echo('暂无轮播图')
        res.send({
            ststus: 0,
            message: '首页轮播图获取成功',
            data: result
        })
    })
}

// 资讯管理
//获取资讯信息
exports.getarticle = (req,res) => {
    const sqlstr = 'select * from yeb_article'
    db.query(sqlstr,(err,result)=>{
        if(err) return res.echo(err)
        if(result.length == 0) return res.echo('暂无资讯信息')
        res.send({
            status: 0,
            message:'资讯列表获取成功',
            data: result
        })
    })
}

// 发布资讯
exports.publishArticle = (req,res) => {
    const bodyinfo = req.body
    const sqlstr = 'insert into yeb_article set ?'
    db.query(sqlstr,bodyinfo,(err,result)=>{
        if(err) return res.echo(err)
        if(result.affectedRows ===0 ) return res.echo('资讯发布失败') 
        res.echo('资讯发布成功',0)
    })
}

// 编辑资讯
exports.changeArticle = (req,res) => {
    const bodyinfo = req.body
    const sqlstr = 'update yeb_article set title=?,content=?,publish_img=?,publish_time=? where id =?'
    db.query(sqlstr,[bodyinfo.title,bodyinfo.content,bodyinfo.publish_img,bodyinfo.publish_time,bodyinfo.id],(err,result)=>{
        if(err) return res.echo(err)
        if(result.affectedRows ===0 ) return res.echo('资讯编辑失败') 
        res.echo('资讯编辑成功',0)
    })
}

// 删除资讯
exports.deleteArticle =(req,res) =>{
    const sqlstr = 'delete from yeb_article where id =?'
    db.query(sqlstr,req.body.id,(err,result)=>{ 
        if(err) return res.echo(err) 
        if(result.affectedRows !== 1) return res.echo('资讯删除失败')
        res.echo('资讯删除成功',0)
    })   
}

// 通过id获取资讯
exports.getArticlebyid = (req,res) => { 
    const sqlstr = 'select * from yeb_article where id = ?'
    db.query(sqlstr,req.body.id,(err,result) => {
        if(err) return res.echo(err) 
        if(result.length === 0) res.echo('暂无资讯详情')
        res.send({ 
            status:'0',
            message:'资讯查询成功',
            data:result[0]
        })
    })
}
// 获取收藏的资讯
exports.getstarzixun = (req,res) => {
    const sqlstr = 'select * from yeb_article where collect_num = 1'
    db.query(sqlstr,(err,result)=> {
        if(err) return res.echo(err)
        if(result.length === 0) return res.echo('暂无收藏资讯')
        res.send({
            status: 0,
            messgae:"收藏资讯查询成功",
            data: result
        })
    })
}
// 根据id增加资讯阅读量
exports.addreadByid = (req,res) =>{
    // console.log(req.body.id)
    const sqlstr = 'select * from yeb_article where id =?'
    db.query(sqlstr,req.body.id,(err,result) => {
        if(err) return res.echo('1')
        if(result.length === 0) return res.echo('资讯获取失败')
        const sqlstr2 = 'update yeb_article set read_num=? where id=?'
        result[0].read_num ++
        db.query(sqlstr2,[result[0].read_num,req.body.id],(err,result)=>{
            if(err) return res.echo(err)
            if(result.affectedRows === 0) return res.echo('阅读数据出错')
            res.echo('阅读数增加成功！',0)
        })
    })
}

// 根据id增加资讯点赞量
exports.addlikeByid = (req,res) =>{
    // console.log(req.body.id)
    const sqlstr = 'select * from yeb_article where id =?'
    db.query(sqlstr,req.body.id,(err,result) => {
        if(err) return res.echo(err)
        if(result.length === 0) return res.echo('资讯获取失败')
        if(result[0].like_num == 0){
            const sqlstr2 = 'update yeb_article set like_num = 1 where id=?'
            db.query(sqlstr2,req.body.id,(err,result)=>{
                if(err) return res.echo(err)
                if(result.affectedRows === 0) return res.echo('点赞数据出错')
                res.echo('已点赞！',0)
            })
        } else {
            const sqlstr2 = 'update yeb_article set like_num = 0 where id=?'
            db.query(sqlstr2,req.body.id,(err,result)=>{
                if(err) return res.echo(err)
                if(result.affectedRows === 0) return res.echo('点赞数据出错')
                res.echo('已取消点赞！',0)
            })
        }
       
    })
}

// 根据id增加资讯评论量
exports.addcommentByid = (req,res) =>{
    // console.log(req.body.id)
    const sqlstr = 'select * from yeb_article where id =?'
    db.query(sqlstr,req.body.id,(err,result) => {
        if(err) return res.echo('1')
        if(result.length === 0) return res.echo('资讯获取失败')
        const sqlstr2 = 'update yeb_article set comment_num =? where id=?'
        result[0].comment_num ++
        db.query(sqlstr2,[result[0].comment_num,req.body.id],(err,result)=>{
            if(err) return res.echo(err)
            if(result.affectedRows === 0) return res.echo('评论数据出错')
            res.echo('评论数增加成功！',0)
        })
    })
}

// 添加评论
exports.addcomment = (req,res) => {
    const comment_content = {...req.body,user_id:req.user.id,comment_user:req.user.username,user_nickname:req.user.nickname}
    const sqlstr = 'insert into yeb_comment set ?'
    db.query(sqlstr,comment_content,(err,result) => {
        if(err) return res.echo(err) 
        if(result.affectedRows === 0) return res.echo('评论失败')
        res.echo('评论发布成功',0)
    })
}
// 查询评论内容
exports.getcomment = (req,res) => {
    const sqlstr = 'select * from yeb_comment where store_id = ?'
    db.query(sqlstr,req.body.store_id,(err,result)=> {
        if(err) return res.echo(err) 
        if(result.length === 0) return res.echo('暂无评论内容')
        res.send({
            status:0,
            message:'评论查询成功',
            data: result
        })
    })
}

// 收藏或取消收藏
exports.addcollectByid = (req,res) =>{
    // console.log(req.body.id)
    const sqlstr = 'select * from yeb_article where id =?'
    db.query(sqlstr,req.body.id,(err,result) => {
        if(err) return res.echo('1')
        if(result.length === 0) return res.echo('资讯获取失败')
        if(result[0].collect_num === 0) { 
            const sqlstr2 = 'update yeb_article set collect_num =1 where id=?'
            db.query(sqlstr2,req.body.id,(err,result)=>{
                if(err) return res.echo(err)
                if(result.affectedRows === 0) return res.echo('收藏数据出错')
                res.echo('已收藏！',0)
            })
        } else {
            const sqlstr2 = 'update yeb_article set collect_num = 0 where id=?'
            db.query(sqlstr2,req.body.id,(err,result)=>{
                if(err) return res.echo(err)
                if(result.affectedRows === 0) return res.echo('收藏数据出错')
                res.echo('已取消收藏！',0)
            })
        }
        
    })
}

// 增加收货地址
exports.addAddress = (req,res) => {
    const address = {...req.body,userid:req.user.id}
    const sqlstr = 'insert into yeb_address set ?'
    db.query(sqlstr,address,(err,result)=> {
        if(err) return res.echo(err) 
        if(result.affectedRows === 0) return res.echo('数据插入失败')
        res.echo('收货地址新增成功',0)

    })
}

// 获取收货地址
exports.getaddress = (req,res)=>{
    const sqlstr = 'select * from yeb_address where userid = ?'
    db.query(sqlstr,req.user.id,(err,result)=>{ 
       if(err) return res.echo(err) 
       if(result.length == 0 ) return res.echo('暂无收货地址')
       res.send({
           status:0,
           message:'收获地址获取成功！',
           data:result
       }) 
    })
}


// 根据id获取收获地址
exports.getaddressByid = (req,res)=>{
    const sqlstr = 'select * from yeb_address where id=?'
    db.query(sqlstr,req.body.id,(err,result)=>{ 
       if(err) return res.echo(err) 
       if(result.length == 0 ) return res.echo('暂无收货地址')
       res.send({
           status:0,
           message:'收获地址获取成功！',
           data:result[0]
       }) 
    })
}

// 删除收货地址
exports.deleteaddress = (req,res) => {
    const sqlstr = 'delete from yeb_address where id = ?'
    db.query(sqlstr,req.body.id,(err,result)=> {
        if(err) return res.echo(err) 
        if(result.affectedRows === 0) return res.echo('删除地址失败')
        res.echo('删除成功！',0)
    })
}